{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fourth-teacher",
   "metadata": {},
   "source": [
    "This is End-to-End example how to search a subnet from ofa/resnet50 design space with constraint of Ops, then quantizing/compiling it with target device of ZCU102 board. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "beginning-farmer",
   "metadata": {},
   "source": [
    "## build ofa resnet50 design space"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "expensive-twist",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Downloading: \"https://hanlab.mit.edu/files/OnceForAll/ofa_nets/ofa_resnet50_d=0+1+2_e=0.2+0.25+0.35_w=0.65+0.8+1.0\" to .torch/ofa_nets/ofa_resnet50_d=0+1+2_e=0.2+0.25+0.35_w=0.65+0.8+1.0\n"
     ]
    }
   ],
   "source": [
    "from ofa.model_zoo import ofa_net\n",
    "ofa_network = ofa_net('ofa_resnet50', pretrained=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "brutal-impossible",
   "metadata": {},
   "source": [
    "##  build accuracy predictor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "racial-prevention",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Downloading: \"https://hanlab.mit.edu/files/OnceForAll/tutorial/ofa_resnet50_acc_predictor.pth\" to /home/vitis-ai-user/.ofa/ofa_resnet50_acc_predictor.pth\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loaded checkpoint from /home/vitis-ai-user/.ofa/ofa_resnet50_acc_predictor.pth\n",
      "The accuracy predictor is ready!\n",
      "AccuracyPredictor(\n",
      "  (layers): Sequential(\n",
      "    (0): Sequential(\n",
      "      (0): Linear(in_features=82, out_features=400, bias=True)\n",
      "      (1): ReLU(inplace=True)\n",
      "    )\n",
      "    (1): Sequential(\n",
      "      (0): Linear(in_features=400, out_features=400, bias=True)\n",
      "      (1): ReLU(inplace=True)\n",
      "    )\n",
      "    (2): Sequential(\n",
      "      (0): Linear(in_features=400, out_features=400, bias=True)\n",
      "      (1): ReLU(inplace=True)\n",
      "    )\n",
      "    (3): Linear(in_features=400, out_features=1, bias=False)\n",
      "  )\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "from ofa.nas.accuracy_predictor import ResNetArchEncoder\n",
    "from ofa.nas.accuracy_predictor import AccuracyPredictor \n",
    "from ofa.utils import download_url\n",
    "\n",
    "image_size_list = [128, 144, 160, 176, 192, 224, 240, 256]\n",
    "arch_encoder = ResNetArchEncoder(\n",
    "\timage_size_list=image_size_list, depth_list=ofa_network.depth_list, expand_list=ofa_network.expand_ratio_list,\n",
    "    width_mult_list=ofa_network.width_mult_list, base_depth_list=ofa_network.BASE_DEPTH_LIST)\n",
    "\n",
    "#ofa/utils/common_tools.py\n",
    "acc_predictor_checkpoint_path = download_url(\n",
    "    'https://hanlab.mit.edu/files/OnceForAll/tutorial/ofa_resnet50_acc_predictor.pth',\n",
    "    model_dir='~/.ofa/',\n",
    ")\n",
    "\n",
    "\n",
    "device = 'cuda:0' if torch.cuda.is_available() else 'cpu'\n",
    "acc_predictor = AccuracyPredictor(arch_encoder, 400, 3,\n",
    "                                  checkpoint_path=acc_predictor_checkpoint_path, device=device)\n",
    "\n",
    "print('The accuracy predictor is ready!')\n",
    "print(acc_predictor)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "actual-flood",
   "metadata": {},
   "source": [
    "## build efficiency predictor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "surrounded-faith",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<ofa.nas.efficiency_predictor.ResNet50FLOPsModel object at 0x7f07dbc7dd50>\n"
     ]
    }
   ],
   "source": [
    "from ofa.nas.efficiency_predictor import ResNet50FLOPsModel\n",
    "\n",
    "efficiency_predictor = ResNet50FLOPsModel(ofa_network)\n",
    "\n",
    "print(efficiency_predictor)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "hybrid-alloy",
   "metadata": {},
   "source": [
    "## build evolution finder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "future-flour",
   "metadata": {},
   "outputs": [],
   "source": [
    "import argparse\n",
    "parser = argparse.ArgumentParser()\n",
    "\n",
    "args = parser.parse_args(args=[])\n",
    "args.arch_mutate_prob = 0.1 \n",
    "args.resolution_mutate_prob = 0.5 \n",
    "args.population_size = 100 \n",
    "args.max_time_budget = 50 \n",
    "args.parent_ratio = 0.25 \n",
    "args.mutation_ratio = 0.5 \n",
    "\n",
    "from ofa.nas.search_algorithm import EvolutionFinder\n",
    "\n",
    "evolution_finder = EvolutionFinder(efficiency_predictor, acc_predictor, **args.__dict__)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "excessive-amplifier",
   "metadata": {},
   "source": [
    "## search best subnet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "confidential-montana",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Generate random population...\n",
      "Start Evolution...\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Searching with constraint (2000): 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 50/50 [00:07<00:00,  6.26it/s, acc=0.821]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0.8209745287895203, {'d': [2, 2, 1, 2, 2], 'e': [0.25, 0.35, 0.35, 0.25, 0.25, 0.25, 0.25, 0.35, 0.25, 0.35, 0.35, 0.35, 0.35, 0.25, 0.35, 0.35, 0.25, 0.25], 'w': [0, 2, 0, 1, 1, 2], 'image_size': 160}, 1984.9216)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# get best subnet with constraint(Mflops)\n",
    "# constraint : Mega flops\n",
    "_, best_info = evolution_finder.run_evolution_search(constraint=2000, verbose=True)\n",
    "print(best_info)\n",
    "predicted_acc, arch_dict, efficiency = best_info"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dutch-hampton",
   "metadata": {},
   "source": [
    "## save net_config for the subnet"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bright-pillow",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "\n",
    "with open('./models/net_config_resnet50_fp2000.pickle', 'wb') as handle:\n",
    "    pickle.dump(arch_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dominican-safety",
   "metadata": {},
   "source": [
    "## upload the saved subnet (when you need to load the saved model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "verbal-mexico",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "import pickle\n",
    "\n",
    "with open('./models/net_config_resnet50_fp2000.pickle', 'rb') as handle:\n",
    "    arch_dict = pickle.load(handle)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "endless-oriental",
   "metadata": {},
   "source": [
    "## extract subnet's weight"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "finished-providence",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Color jitter: tf, resize_scale: 0.08, img_size: [128, 144, 160, 176, 192, 224, 240, 256]\n",
      "Use MyRandomResizedCrop: [128, 144, 160, 176, 192, 224, 240, 256], \t None sync=True, continuous=False\n",
      "ResNets(\n",
      "  (input_stem): ModuleList(\n",
      "    (0): ConvLayer(\n",
      "      (conv): Conv2d(3, 24, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
      "      (bn): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      (act): ReLU(inplace=True)\n",
      "    )\n",
      "    (1): ResidualBlock(\n",
      "      (conv): ConvLayer(\n",
      "        (conv): Conv2d(24, 24, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (shortcut): IdentityLayer()\n",
      "    )\n",
      "    (2): ConvLayer(\n",
      "      (conv): Conv2d(24, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "      (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      (act): ReLU(inplace=True)\n",
      "    )\n",
      "  )\n",
      "  (max_pooling): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)\n",
      "  (blocks): ModuleList(\n",
      "    (0): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(64, 40, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(40, 40, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(40, 168, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(168, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): Sequential(\n",
      "        (avg_pool): AvgPool2d(kernel_size=1, stride=1, padding=0)\n",
      "        (conv): Conv2d(64, 168, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(168, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (1): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(168, 56, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(56, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(56, 56, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(56, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(56, 168, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(168, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (2): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(168, 56, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(56, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(56, 56, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(56, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(56, 168, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(168, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (3): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(168, 40, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(40, 40, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(40, 168, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(168, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (4): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(168, 104, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(104, 104, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(104, 408, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(408, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): Sequential(\n",
      "        (avg_pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n",
      "        (conv): Conv2d(168, 408, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(408, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (5): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(408, 104, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(104, 104, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(104, 408, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(408, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (6): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(408, 104, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(104, 104, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(104, 408, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(408, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (7): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(408, 208, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(208, 208, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(208, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): Sequential(\n",
      "        (avg_pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n",
      "        (conv): Conv2d(408, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (8): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(288, 288, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(288, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (9): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(288, 288, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(288, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (10): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(288, 288, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(288, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (11): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(288, 288, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(288, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (12): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 208, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(208, 208, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(208, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (13): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 720, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(720, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(720, 720, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(720, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(720, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): Sequential(\n",
      "        (avg_pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n",
      "        (conv): Conv2d(816, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (14): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(2048, 720, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(720, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(720, 720, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(720, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(720, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (15): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (16): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "  )\n",
      "  (global_avg_pool): AdaptiveAvgPool2d(output_size=1)\n",
      "  (classifier): LinearLayer(\n",
      "    (linear): Linear(in_features=2048, out_features=1000, bias=True)\n",
      "  )\n",
      ")\n",
      "Total training params: 34.37M\n",
      "Total FLOPs: 5078.20M\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Validate Epoch #1 : 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 15/15 [00:09<00:00,  1.61it/s, loss=0.979, top1=77, top5=93.9, img_size=160]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Test acc: 77.000\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "\n",
    "ofa_network.set_active_subnet(**arch_dict)\n",
    "subnet = ofa_network.get_active_subnet(preserve_weight=True)  \n",
    "\n",
    "from ofa.imagenet_classification.run_manager import ImagenetRunConfig\n",
    "from ofa.imagenet_classification.run_manager import RunManager\n",
    "\n",
    "run_config = ImagenetRunConfig(test_batch_size=200, n_worker=4, image_size=image_size_list, valid_size=1000)\n",
    "run_manager = RunManager('.tmp/eval_subnet', subnet, run_config, init=False)\n",
    "\n",
    "run_manager.run_config.data_provider.assign_active_img_size(arch_dict['image_size'])\n",
    "run_manager.reset_running_statistics(subnet, subset_size=1000, subset_batch_size=250)\n",
    "\n",
    "# evaluate subnet on validation dataset\n",
    "_, (top1, _) = run_manager.validate(is_test=True)\n",
    "#print('Test acc: %.3f,\\t best_info: %s' % (top1, best_info))\n",
    "print('Test acc: %.3f' % (top1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "sophisticated-andrews",
   "metadata": {},
   "source": [
    "## save the subnet's weight "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "wireless-florence",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ResNets(\n",
      "  (input_stem): ModuleList(\n",
      "    (0): ConvLayer(\n",
      "      (conv): Conv2d(3, 24, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
      "      (bn): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      (act): ReLU(inplace=True)\n",
      "    )\n",
      "    (1): ResidualBlock(\n",
      "      (conv): ConvLayer(\n",
      "        (conv): Conv2d(24, 24, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (shortcut): IdentityLayer()\n",
      "    )\n",
      "    (2): ConvLayer(\n",
      "      (conv): Conv2d(24, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "      (bn): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      (act): ReLU(inplace=True)\n",
      "    )\n",
      "  )\n",
      "  (max_pooling): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)\n",
      "  (blocks): ModuleList(\n",
      "    (0): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(64, 40, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(40, 40, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(40, 168, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(168, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): Sequential(\n",
      "        (avg_pool): AvgPool2d(kernel_size=1, stride=1, padding=0)\n",
      "        (conv): Conv2d(64, 168, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(168, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (1): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(168, 56, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(56, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(56, 56, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(56, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(56, 168, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(168, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (2): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(168, 56, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(56, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(56, 56, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(56, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(56, 168, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(168, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (3): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(168, 40, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(40, 40, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(40, 168, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(168, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (4): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(168, 104, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(104, 104, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(104, 408, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(408, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): Sequential(\n",
      "        (avg_pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n",
      "        (conv): Conv2d(168, 408, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(408, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (5): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(408, 104, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(104, 104, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(104, 408, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(408, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (6): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(408, 104, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(104, 104, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(104, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(104, 408, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(408, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (7): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(408, 208, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(208, 208, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(208, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): Sequential(\n",
      "        (avg_pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n",
      "        (conv): Conv2d(408, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (8): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(288, 288, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(288, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (9): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(288, 288, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(288, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (10): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(288, 288, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(288, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (11): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 288, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(288, 288, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(288, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(288, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (12): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 208, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(208, 208, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(208, 816, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(816, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (13): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(816, 720, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(720, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(720, 720, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(720, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(720, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): Sequential(\n",
      "        (avg_pool): AvgPool2d(kernel_size=2, stride=2, padding=0)\n",
      "        (conv): Conv2d(816, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (14): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(2048, 720, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(720, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(720, 720, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(720, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(720, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (15): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "    (16): ResNetBottleneckBlock(\n",
      "      (conv1): Sequential(\n",
      "        (conv): Conv2d(2048, 512, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv2): Sequential(\n",
      "        (conv): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "        (act): ReLU(inplace=True)\n",
      "      )\n",
      "      (conv3): Sequential(\n",
      "        (conv): Conv2d(512, 2048, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
      "        (bn): BatchNorm2d(2048, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n",
      "      )\n",
      "      (downsample): IdentityLayer()\n",
      "      (final_act): ReLU(inplace=True)\n",
      "    )\n",
      "  )\n",
      "  (global_avg_pool): AdaptiveAvgPool2d(output_size=1)\n",
      "  (classifier): LinearLayer(\n",
      "    (linear): Linear(in_features=2048, out_features=1000, bias=True)\n",
      "  )\n",
      ")\n"
     ]
    }
   ],
   "source": [
    "print(subnet)\n",
    "torch.save(subnet.state_dict(), './models/resnet50_fp2000.pth', _use_new_zipfile_serialization=False)        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bridal-certificate",
   "metadata": {},
   "source": [
    "## quantize the model (subnet)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "weekly-advocate",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "160\n"
     ]
    }
   ],
   "source": [
    "# find the input image size of the subnet\n",
    "print(arch_dict['image_size'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "considerable-dialogue",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Loading NNDCT kernels...\u001b[0m\n",
      "-------- Start resnet50_fp2000 test \n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: OS and CPU information:\n",
      "               system --- Linux\n",
      "                 node --- xsjfislx20\n",
      "              release --- 4.15.0-166-generic\n",
      "              version --- #174-Ubuntu SMP Wed Dec 8 19:07:44 UTC 2021\n",
      "              machine --- x86_64\n",
      "            processor --- x86_64\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Tools version information:\n",
      "                  GCC --- GCC 9.4.0\n",
      "               python --- 3.7.12\n",
      "              pytorch --- 1.12.1\n",
      "        vai_q_pytorch --- 3.0.0+fec926f+torch1.12.1\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: GPU information:\n",
      "          device name --- Tesla V100-PCIE-16GB\n",
      "     device available --- True\n",
      "         device count --- 3\n",
      "       current device --- 0\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Quant config file is empty, use default quant configuration\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Quantization calibration process start up...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Quant Module is in 'cuda'.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Parsing ResNets...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Start to trace and freeze model...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: The input model ResNets is torch.nn.Module.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Finish tracing.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Processing ops...\u001b[0m\n",
      "██████████████████████████████████████████████████| 198/198 [00:00<00:00, 2787.9\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Doing weights equalization...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Quantizable module is generated.(quantize_result/ResNets.py)\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Get module with quantization.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Preparing data for fast finetuning module parameters ...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Mem status(total mem: 92.89G, available mem: 81.09G).\u001b[0m\n",
      "100%|███████████████████████████████████████████| 32/32 [00:08<00:00,  3.59it/s]\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Memory cost by fast finetuning is 0.43 G.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Find initial quantization steps for fast finetuning...\u001b[0m\n",
      "100%|███████████████████████████████████████████| 32/32 [00:14<00:00,  2.21it/s]\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Fast finetuning module parameters for better quantization accuracy...\u001b[0m\n",
      "100%|███████████████████████████████████████████| 59/59 [23:16<00:00, 23.67s/it]\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Export fast finetuned parameters ...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Exporting quant model parameters.(quantize_result/param.pth)\u001b[0m\n",
      "100%|███████████████████████████████████████████| 94/94 [00:30<00:00,  3.04it/s]\n",
      "loss: 0.0475976\n",
      "top-1 / top-5 accuracy: 78.1 / 94.2333\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Exporting quant config.(quantize_result/quant_info.json)\u001b[0m\n",
      "-------- End of resnet50_fp2000 test \n"
     ]
    }
   ],
   "source": [
    "# quantization with finetune \n",
    "!python ofa_quant.py --model_name 'resnet50_fp2000' --image_size 192 --quant_mode calib --fast_finetune\n",
    "\n",
    "# general quantization\n",
    "#!python ofa_quant.py --model_name 'resnet50_fp2000' --image_size 192 --quant_mode calib --subset_len 200"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "korean-edmonton",
   "metadata": {},
   "source": [
    "## evaluate the quantized model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "increasing-domain",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Loading NNDCT kernels...\u001b[0m\n",
      "-------- Start resnet50_fp2000 test \n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: OS and CPU information:\n",
      "               system --- Linux\n",
      "                 node --- xsjfislx20\n",
      "              release --- 4.15.0-166-generic\n",
      "              version --- #174-Ubuntu SMP Wed Dec 8 19:07:44 UTC 2021\n",
      "              machine --- x86_64\n",
      "            processor --- x86_64\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Tools version information:\n",
      "                  GCC --- GCC 9.4.0\n",
      "               python --- 3.7.12\n",
      "              pytorch --- 1.12.1\n",
      "        vai_q_pytorch --- 3.0.0+fec926f+torch1.12.1\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: GPU information:\n",
      "          device name --- Tesla V100-PCIE-16GB\n",
      "     device available --- True\n",
      "         device count --- 3\n",
      "       current device --- 0\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Quant config file is empty, use default quant configuration\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Quantization test process start up...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Quant Module is in 'cuda'.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Parsing ResNets...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Start to trace and freeze model...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: The input model ResNets is torch.nn.Module.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Finish tracing.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Processing ops...\u001b[0m\n",
      "██████████████████████████████████████████████████| 198/198 [00:00<00:00, 2985.0\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Doing weights equalization...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Quantizable module is generated.(quantize_result/ResNets.py)\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Get module with quantization.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Loading quant model parameters.(quantize_result/param.pth)\u001b[0m\n",
      "100%|███████████████████████████████████████████| 94/94 [00:28<00:00,  3.26it/s]\n",
      "loss: 0.0475976\n",
      "top-1 / top-5 accuracy: 78.1 / 94.2333\n",
      "-------- End of resnet50_fp2000 test \n"
     ]
    }
   ],
   "source": [
    "# evaluation with finetune\n",
    "!python ofa_quant.py --model_name 'resnet50_fp2000' --image_size 192 --quant_mode test --fast_finetune\n",
    "\n",
    "# general evaluation\n",
    "#!python ofa_quant.py --model_name 'resnet50_fp2000' --image_size 192 --quant_mode test\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "auburn-composite",
   "metadata": {},
   "source": [
    "## Export the quantized model as xmodel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "gentle-hungary",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Loading NNDCT kernels...\u001b[0m\n",
      "-------- Start resnet50_fp2000 test \n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: OS and CPU information:\n",
      "               system --- Linux\n",
      "                 node --- xsjfislx20\n",
      "              release --- 4.15.0-166-generic\n",
      "              version --- #174-Ubuntu SMP Wed Dec 8 19:07:44 UTC 2021\n",
      "              machine --- x86_64\n",
      "            processor --- x86_64\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Tools version information:\n",
      "                  GCC --- GCC 9.4.0\n",
      "               python --- 3.7.12\n",
      "              pytorch --- 1.12.1\n",
      "        vai_q_pytorch --- 3.0.0+fec926f+torch1.12.1\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: GPU information:\n",
      "          device name --- Tesla V100-PCIE-16GB\n",
      "     device available --- True\n",
      "         device count --- 3\n",
      "       current device --- 0\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Quant config file is empty, use default quant configuration\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Quantization test process start up...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Quant Module is in 'cuda'.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Parsing ResNets...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Start to trace and freeze model...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: The input model ResNets is torch.nn.Module.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Finish tracing.\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: Processing ops...\u001b[0m\n",
      "██████████████████████████████████████████████████| 198/198 [00:00<00:00, 2986.2\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Doing weights equalization...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Quantizable module is generated.(quantize_result/ResNets.py)\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Get module with quantization.\u001b[0m\n",
      "100%|█████████████████████████████████████████████| 1/1 [00:00<00:00,  4.85it/s]\n",
      "loss: 0.746426\n",
      "top-1 / top-5 accuracy: 100 / 100\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Converting to xmodel ...\u001b[0m\n",
      "\n",
      "\u001b[0;32m[VAIQ_NOTE]: =>Successfully convert 'ResNets' to xmodel.(quantize_result/ResNets_int.xmodel)\u001b[0m\n",
      "-------- End of resnet50_fp2000 test \n"
     ]
    }
   ],
   "source": [
    "\n",
    "!python ofa_quant.py --model_name 'resnet50_fp2000' --image_size 192 --quant_mode test --subset_len 1 --batch_size=1 --deploy\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "unlike-harmony",
   "metadata": {},
   "source": [
    "## Compile the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "experimental-banks",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "**************************************************\n",
      "* VITIS_AI Compilation - Xilinx Inc.\n",
      "**************************************************\n",
      "[UNILOG][INFO] Compile mode: dpu\n",
      "[UNILOG][INFO] Debug mode: null\n",
      "[UNILOG][INFO] Target architecture: DPUCZDX8G_ISA1_B4096\n",
      "[UNILOG][INFO] Graph name: ResNets, with op num: 472\n",
      "[UNILOG][INFO] Begin to compile...\n",
      "\u001b[0;33m[UNILOG][WARNING] xir::Op{name = ResNets__ResNets_ResNetBottleneckBlock_blocks__ModuleList_0__Sequential_downsample__AvgPool2d_avg_pool__input_19_fix, type = pool-fix}'s input and output is unchanged, so it will be removed.\n",
      "\u001b[m[UNILOG][INFO] Total device subgraph number 3, DPU subgraph number 1\n",
      "[UNILOG][INFO] Compile done.\n",
      "[UNILOG][INFO] The meta json is saved to \"/workspace/examples/ofa/ofa_resnet50/compiled/meta.json\"\n",
      "[UNILOG][INFO] The compiled xmodel is saved to \"/workspace/examples/ofa/ofa_resnet50/compiled/resnet50_fp2000.xmodel\"\n",
      "[UNILOG][INFO] The compiled xmodel's md5sum is 27f6954d29fa3333d5b6d0b39a8efc0b, and has been saved to \"/workspace/examples/ofa/ofa_resnet50/compiled/md5sum.txt\"\n"
     ]
    }
   ],
   "source": [
    "# target hardware: ZCu102\n",
    "!vai_c_xir -x quantize_result/ResNets_int.xmodel -a /opt/vitis_ai/compiler/arch/DPUCZDX8G/ZCU102/arch.json -o compiled -n resnet50_fp2000\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "looking-morris",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
